package com.scalar.chiptrack.operations.wipflush.actions;

import com.scalar.chiptrack.ChipTrackExtendedAction;
import com.scalar.chiptrack.operations.wipflush.actioncommands.WipFlushSetupActionCommandFactory;
import com.scalar.chiptrack.users.ErrorReport;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.actioncommands.ActionCommand;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: Oct 24, 2005
 * Time: 1:52:59 PM
 * To change this template use Options | File Templates.
 */
public class WipFlushSetupAction extends ChipTrackExtendedAction
{
    public ActionForward executeRequest(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        HashMap errors  = ( HashMap ) request.getAttribute( ERROR_MESSAGES );
		HashMap info    = ( HashMap ) request.getAttribute( INFO_MESSAGES );
        String action   = request.getParameter( REQUEST_ACTION );
        if ( errors == null )
        {
            errors = new HashMap();
            request.setAttribute( ERROR_MESSAGES, errors );
        }
        if ( info == null )
        {
            info = new HashMap();
            request.setAttribute( INFO_MESSAGES, info );
        }
        if ( action == null )
        {
            action = ACTION_LOAD_BASE_PRODUCTS ;
        }
        //System.out.println("--------------WF-ACTION-1");
        ActionCommand actionCommand = WipFlushSetupActionCommandFactory.getActionCommand( action );
        //System.out.println("--------------WF-ACTION-2");
        ActionForward actionForward = null;
        if( actionCommand == null )
        {
            Logger.error( "Could not create action instance" );
            errors.put( ERROR_INTERNAL_SERVER_ERROR, "Could not create action instance" );
            actionForward = mapping.findForward( "success" );
        }
        else
        {
            try
            {
                //System.out.println("--------------WF-ACTION-3");
                actionForward = actionCommand.execute( mapping, form, request, response );
            }
            catch ( Exception e )
            {
                e.printStackTrace();
                Logger.error( "Error while executing action command", e );
                errors.put( ERROR_INTERNAL_SERVER_ERROR, "Error while executing action command" );
                pSession.setAttribute( ERROR_REPORT_OBJECT, new ErrorReport( "Error while executing the action command", e ) );
                actionForward = mapping.findForward( "success" );
            }
        }
        return actionForward;
    }
}
